package cn.iocoder.yudao.module.xkfz.service.excel.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.ScoreCalculationData;
import cn.iocoder.yudao.module.xkfz.domain.excel.entity.ProjectInfo;
import cn.iocoder.yudao.module.xkfz.domain.excel.po.ProjectInfoPo;
import cn.iocoder.yudao.module.xkfz.mapper.excel.ProjectInfoMapper;
import cn.iocoder.yudao.module.xkfz.mapper.rules.GroupTypeMapper;
import cn.iocoder.yudao.module.xkfz.service.excel.ProjectInfoService;
import cn.iocoder.yudao.module.xkfz.service.reporting.ScoreCalculationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author ZWC
 * @Date 2024/5/14 16:05
 */
@Service
@Slf4j
public class ProjectInfoImpl implements ProjectInfoService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private GroupTypeMapper groupTypeMapper;

    @Autowired
    private ScoreCalculationService scoreCalculationService;

    private String INDEX_ID = "?";
    @Override
    public Boolean importProjectInfo(List<ProjectInfo> list, String year, String indexId) {
        INDEX_ID = indexId;
        projectInfoMapper.delete(ProjectInfo::getPeriodTime,year);
        if (CollectionUtil.isNotEmpty(list)){
            for (ProjectInfo projectInfo : list) {
                projectInfo.setId(IdUtil.getSnowflakeNextId());
                projectInfo.setPeriodTime(year);
            };
            if (projectInfoMapper.insertBatch(list,500)) {
                addScoreData(year);
            }
        }
        return true;
    }


    /**
     * 功能描述: 计算某一年份的得分数据并插入到得分表中
     * @Param:
     * @Return: 返回插入得分表的条数
     * @Author: ZWC
     * @Date: 2024/5/20
     */
    private Integer addScoreData(String year){
        //合并两张表的查询数据
        List<ScoreCalculationData> countDataList = groupTypeMapper.getProjectInfoCount(year);
        Integer num = scoreCalculationService.selectionRule(INDEX_ID, year, countDataList);
        log.info("插入得分表数据条数{}，年份{}",num,year);
        return num;
    }

    @Override
    public PageResult<ProjectInfo> selectPage(ProjectInfoPo reqVO) {
        return projectInfoMapper.selectPage(reqVO);
    }

    @Override
    public Boolean insert(ProjectInfo req) {
        if (projectInfoMapper.insert(req) > 0) {
            addScoreData(req.getPeriodTime());
        }
        return true;
    }

    @Override
    public Boolean update(ProjectInfo req) {
        if (projectInfoMapper.updateById(req) > 0) {
            addScoreData(req.getPeriodTime());
        }
        return true;
    }


    @Override
    public Boolean delete(String id) {
        ProjectInfo projectInfo = projectInfoMapper.selectById(id);
        if (projectInfoMapper.deleteById(id) > 0) {
            addScoreData(projectInfo.getPeriodTime());
        }
        return true;
    }

}
